//
//  TopSegmentView.m
//  Marketing
//
//  Created by Hanen 3G 01 on 16/5/11.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "TopSegmentView.h"
#define DefaultCurrentBtnColor [UIColor whiteColor]
@interface TopSegmentView ()
{
    NSInteger   _btnCount;
    
    UIView     *_indicatorView; // 指示视图(滑动视图)
    
    UIButton   *_currentBtn;
    
    BOOL        _isSelectedBegan; // 是否设置了selectedBegan
    
    NSInteger   _currentIndex;
    
    CGFloat     _btnWidth;
    
    CGFloat     _btnHeight;
}

@property (nonatomic, strong)NSMutableArray *btnArray;
@end

@implementation TopSegmentView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.layer.cornerRadius  = frame.size.height / 2;
        self.backgroundColor = [UIColor colorWithRed:53.0f / 255.0f  green:63.0f / 255.0f  blue:130.0f / 255.0f  alpha:1];
        self.layer.masksToBounds = YES;
//        self.titles = titles;
        _btnCount = 2;
        
        [self creatView];
    }
    return self;
}
//lazyLoad
- (NSMutableArray *)btnArray
{
    if (_btnArray == nil) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}
- (void)setIndicatorViewColor:(UIColor *)indicatorViewColor
{
    _indicatorViewColor = indicatorViewColor;
    _indicatorView.backgroundColor = _indicatorViewColor;
    
}

- (void)setLeftAttributeStr:(NSAttributedString *)leftAttributeStr
{
    _leftAttributeStr = leftAttributeStr;
    [self.leftBtn setAttributedTitle:leftAttributeStr forState:UIControlStateNormal];
}

- (void)setRightAttributeStr:(NSAttributedString *)rightAttributeStr
{
    _rightAttributeStr = rightAttributeStr;
    [self.rightBtn setAttributedTitle:rightAttributeStr forState:UIControlStateNormal];
}
- (void)setLeftTitle:(NSString *)leftTitle
{
    _leftTitle = leftTitle;
    
    [self.leftBtn setTitle:self.leftTitle forState: UIControlStateNormal];
}

- (void)setRightTitle:(NSString *)rightTitle
{
    _rightTitle = rightTitle;
    
    [self.rightBtn setTitle:self.rightTitle forState: UIControlStateNormal];
}
- (void)creatView
{
    _btnWidth = self.frame.size.width / 2.0;
    _btnHeight = self.frame.size.height;
    
    if(_firstIndex == 0){
        _indicatorView = [[UIView alloc] init];
        _indicatorView.backgroundColor = _indicatorViewColor;
        _indicatorView.alpha = 1;
        _indicatorView.layer.cornerRadius = self.layer.cornerRadius;
        _indicatorView.layer.masksToBounds = YES;
    }else if(_firstIndex == 1){
    _indicatorView = [[UIView alloc] init];
    _indicatorView.backgroundColor = _indicatorViewColor;
    _indicatorView.alpha = 1;
    _indicatorView.layer.cornerRadius = self.layer.cornerRadius;
    _indicatorView.layer.masksToBounds = YES;
    }
    [self addSubview:_indicatorView];
    
    
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.leftBtn.frame = CGRectMake(0, 0, _btnWidth, _btnHeight);
     [self.leftBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
     self.leftBtn.tag = 95115955;
    [self.leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.rightBtn.frame = CGRectMake(_btnWidth, 0, _btnWidth, _btnHeight);
    self.rightBtn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    self.rightBtn.tag = 95115956;
    [self.rightBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.rightBtn setTitleColor:NavBackColor forState:UIControlStateNormal];
    
    [self addSubview:self.rightBtn];
    [self addSubview:self.leftBtn];
    
    
    // 创建各个按钮
//    for (int i = 0; i < _btnCount; i++)
//    {
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
//        btn.frame = CGRectMake(_btnWidth * i, 0, _btnWidth, _btnHeight);
//        [btn setTitle:self.titles[i] forState:UIControlStateNormal];
//        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
//        
//        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
//        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        btn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
//        
//        btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
//        btn.layer.cornerRadius = self.layer.cornerRadius;
//        
//        btn.tag = 95115955 + i;
//        [self addSubview:btn];
//        
//        [self.btnArray addObject:btn]; // 加入数组
//        
//        
//    }
    
}

- (void)setFirstIndex:(NSInteger)firstIndex
{
    _firstIndex = firstIndex;
    if(firstIndex == 0){
        _indicatorView.frame = CGRectMake(0, 1, _btnWidth, _btnHeight - 2);
    }else{
        _indicatorView.frame = CGRectMake(_btnWidth, 1, _btnWidth, _btnHeight - 2);
    }
    
}
//seg的触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self selectedBegan];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    CGPoint previousTouch = [touch previousLocationInView:self];

    //移动距离
    CGFloat diffMove = point.x - previousTouch.x;

    //限制指示图移动范围
    CGRect frame = _indicatorView.frame;
    if (frame.origin.x + diffMove >= 0 && frame.origin.x + diffMove <= (_btnCount - 1)*_btnWidth ) {
        frame.origin =  CGPointMake(frame.origin.x + diffMove, 0);
    }
    

     _indicatorView.frame = frame;
}
//手势结束判断改滑到哪个button上
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint center = _indicatorView.center;
    NSInteger index = center.x / _btnWidth;
    [UIView animateWithDuration:.35 animations:^{
        _indicatorView.frame = CGRectMake(_btnWidth * index, 0, _btnWidth, _btnHeight);
        [self selectedEnd];
       
    } completion:^(BOOL finished) {
        if ([_delegate respondsToSelector:@selector(segmentView:didScrollIndex:)]) {
            [_delegate segmentView:self didScrollIndex:index];
        }
    }];
    

}
#pragma mark --seg时间处理
//#pragma mark 事件拦截，当点击区域在指示视图的范围内时就直接把事件交给self处理，按钮就接收不到事件了
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGPoint p = [self convertPoint:point toView:_indicatorView];
    if ([_indicatorView pointInside:p withEvent:event]) {
        return self;
    } else {
        return [super hitTest:point withEvent:event];
    }
}
//按钮点击
- (void)btnClicked:(UIButton *)btn
{
    [self selectedBegan];
    _currentIndex = btn.tag - 95115955;
    [UIView animateWithDuration:.35 animations:^{
        _indicatorView.frame = CGRectMake(_btnWidth * _currentIndex, 0, _btnWidth, _btnHeight);
       
        
    } completion:^(BOOL finished) {
         [self selectedEnd];
        if ([_delegate respondsToSelector:@selector(segmentView:didSelectIndex:withTitle:)]) {
            [_delegate segmentView:self didSelectIndex:_currentIndex withTitle:btn.currentTitle];
        }
    }];
    //在动画完成之后去 执行代理的画，指示画面会先显示，然后字体在浮现 ，感觉有些停顿 所以 放一起做。。
}
//** 选开始的设置，指示视图变暗，字体颜色改变 */
- (void)selectedBegan
{
    if (_isSelectedBegan) return;
    
    _isSelectedBegan = YES;
    
    _indicatorView.alpha = 0.5f;
    
}


/** 选开始的设置3333 */
- (void)selectedEnd
{
    if (!_isSelectedBegan) return;
    
    _isSelectedBegan = NO;
    
    _indicatorView.alpha = 1.0f;
    
    // 如果没有设置background，就为默认颜色
//    UIColor *color = self.backgroundColor ? self.backgroundColor : DefaultCurrentBtnColor;
//    [_currentBtn setTitleColor:color forState:UIControlStateNormal];
}

@end
